package com.wsoft.response;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 通用分页查询返回类型
 *
 * @author: mdl
 * @date: 2022/6/29 9:25
 */
@Data
@Schema(description = "用户信息")
public class ResponsePage<T> implements Serializable, ResponsePageCommon {

    @Schema(description = "当前页码", title = "pageNum")
    private Integer pageNum;
    @Schema(description = "每页条数", name = "pageSize")
    private Integer pageSize;
    @Schema(description = "总条数", name = "totalCount")
    private Long totalCount;
    @Schema(description = "总页数", name = "totalPages")
    private Integer totalPages;
    /**
     * 可以添加其他有必要的分页信息属性
     * 查询出的所有对象的集合
     */
    @Schema(description = "分页数据", name = "list")
    private List<T> list;

    /**
     * 下面定义一个方法,实现将PageInfo类型中的对象的值赋值给当前ResponsePage对象对应的属性
     *
     * @param pageInfo
     * @param <T>
     * @return
     */
    public static <T> ResponsePage<T> restPage(PageInfo<T> pageInfo) {
        ResponsePage<T> result = new ResponsePage<>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalCount(pageInfo.getTotal());
        result.setTotalPages(pageInfo.getPages());
        // 将pageInfo中的查询结果结合也要赋值过去
        result.setList(pageInfo.getList());
        return result;

    }

    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> ResponsePage<T> restPage(List<T> list) {
        ResponsePage<T> result = new ResponsePage<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);
        result.setTotalPages(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 转化mybatis plus的分页结果
     */
    public static <T> ResponsePage<T> restPage(IPage<T> page) {
        ResponsePage<T> result = new ResponsePage<>();
        result.setTotalPages((int) page.getPages());
        result.setPageNum((int) page.getCurrent());
        result.setPageSize((int) page.getSize());
        result.setTotalCount(page.getTotal());
        result.setList(page.getRecords());
        return result;
    }

}
